//
//  MHBindInvitedCodeViewController.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/11/10.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHBindInvitedCodeViewController.h"
#import "MHBindInviterModel.h"
#import "MHShareAppViewModel.h"
@interface MHBindInvitedCodeViewController ()
@property(nonatomic, strong)  MHBindInviterModel *model;
@end

@implementation MHBindInvitedCodeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.bindTitle.font = MHRegularFont(28);
    self.invitedCodeTitle.font = MHRegularFont(30);
    self.numbersLabel.font = MHRegularFont(30);
    self.numberTips.font = MHRegularFont_13;//;

    self.bindNameLabel.font = MHFont(16, YES);
    self.invitedBtn.titleLabel.font = MHRegularFont_16;
    self.invitedBtn.layer.cornerRadius = 22;
    self.invitedBtn.masksToBounds = YES;
    self.invitedBtn.userInteractionEnabled = YES;
    self.invitedTextFeild.font = MHRegularFont_15;
//    __weak __typeof(self)weakSelf = self;

    [self.invitedBtn bk_whenTapped:^{
        
        if(self.model.inviterName.length > 0)
        {
            MHShareAppViewModel *shareModel =  [[MHShareAppViewModel alloc] initWithServices:self.viewModel.services params:nil];
            [self.viewModel.services pushViewModel:shareModel animated:YES];
        }else
        {
            if(self.invitedTextFeild.text.length <=0)
            {
                return [UIWindow showTips:@"请输入邀请码"];
            }
            [[[MHHTTPService sharedInstance] bindUserInviteCodeWithInviteCode:self.invitedTextFeild.text] subscribeNext:^(MHBindInviterModel *bindModel) {
                [self configWithBindInviterModel:bindModel];
            }];
        }
        
    }];
    
    [[[MHHTTPService sharedInstance] getUserBindInviteCode]  subscribeNext:^(MHBindInviterModel *model) {
        if (!model) {
        }else
        {
            [self configWithBindInviterModel:model];
        }
        
        
    } error:^(NSError *error) {
       
    }];
}


-(void) configWithBindInviterModel:(MHBindInviterModel *) model
{
    
//    self.invitedBtn.alpha = 0.5;
//    self.invitedBtn.userInteractionEnabled = false;
    self.invitedCodeTitle.text = model.inviteCode;
    self.model = model;
    NSMutableAttributedString *coutAttr=  [[NSMutableAttributedString alloc ] initWithString:[NSString stringWithFormat:@"%d",model.subordinateCount]];
    [coutAttr addAttribute:NSUnderlineStyleAttributeName value:
     [NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, coutAttr.length)];
    self.numbersLabel.attributedText = coutAttr;
    
    if (model.inviterName.length > 0) {
        [self.invitedBtn setTitle:@"立即邀请" forState:0];
        NSMutableAttributedString * bindAttr=  [[NSMutableAttributedString alloc ] initWithString:[NSString stringWithFormat:@"您与%@成功进行绑定\n立即邀请好友加入百唠",model.inviterName]];
//        [coutAttr addAttribute:NSUnderlineStyleAttributeName value:
//         [NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, coutAttr.length)];
        
        [bindAttr addAttribute:NSForegroundColorAttributeName value:MHColorFromHexString(@"#FF7B0B") range:NSMakeRange(2, model.inviterName.length)];
        
        [bindAttr addAttribute:NSForegroundColorAttributeName value:MHColorFromHexString(@"#A89796") range:NSMakeRange(bindAttr.length - 13, 13)];
        
        [bindAttr addAttribute:NSFontAttributeName value:MHRegularFont_12 range:NSMakeRange(bindAttr.length - 13, 13)];
        self.bindNameLabel.attributedText = bindAttr;
        self.textContainerView.hidden = YES;
         self.nameLableBottomConstraint.constant = 40;
    }else
    {
        self.nameLableBottomConstraint.constant = 106;
        [self.invitedBtn setTitle:@"提交" forState:0];
//        self.
    }
    
    
    
    
    
    
//    self.invitedTextFeild.text = [NSString stringWithFormat:@"邀请人:%@  邀请码:%@",model.inviterName,model.inviteCode];
//    self.invitedTextFeild.backgroundColor = MHTEXTCOLORF2;
//    self.invitedTextFeild.borderStyle = 0;
//    self.invitedTextFeild.userInteractionEnabled = false;
}




@end
